package com.kczd.jinlan.view.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.adapter.LocationAdapter;
import com.kczd.jinlan.model.User;
import com.kczd.jinlan.utils.Config;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * yang_g
 * 收货地址
 * Created by Administrator on 2017/5/23.
 */

public class LocationActivity extends Activity implements View.OnClickListener {
    private Button but_add;
    private ListView ls;
    public static List<User> datas = new ArrayList<>();
    String address_id;
    private TextView id_fanhui;
    String url = Config.net_addr + "user/address_list";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_activity);
        initHttp();
    }

    private void initHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getHttpData();
            }
        }).start();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();

        but_add = (Button) findViewById(R.id.but_add);
        but_add.setOnClickListener(this);
        ls = (ListView) findViewById(R.id.list_view);

        id_fanhui = (TextView) findViewById(R.id.id_fanhui);
        id_fanhui.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_add:

                Intent intent = new Intent();
                intent.putExtra("address_id", address_id);
                intent.setClass(this, AddLocationActivity.class);
                startActivity(intent);
                break;
            case R.id.id_fanhui:
                finish();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    public void getHttpData() {
        //创建okHttpClient对象
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("token", Config.user.getToken())
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String strResult = response.body().string();
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("ret_json", strResult);
            msg.setData(data);
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle msgg = msg.getData();
            String val = msgg.getString("ret_json");
            val = val.substring(val.indexOf("{"), val.lastIndexOf("}") + 1);
            JSONObject jsonObject;
            try {
                jsonObject = new JSONObject(val);
                JSONArray ja = jsonObject.getJSONArray("data");
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jb = (JSONObject) ja.get(i);
                    String address = jb.getString("address");
                    Log.e("address", address);

                    address_id = jb.getString("address_id");
                    Log.e("address_id", address_id);

                    JSONObject city = jb.getJSONObject("city");
                    Log.e("city", city + "");
                    String id = city.getString("id");
                    Log.e("id", id);

                    String cityname = city.getString("name");
                    Log.e("cityname", cityname);

                    String consignee = jb.getString("consignee");
                    Log.e("consignee", consignee);

                    String mobile = jb.getString("mobile");
                    Log.e("mobile", mobile);

                    JSONObject district = jb.getJSONObject("district");
                    String districtname = district.getString("name");
                    Log.e("districtname", districtname);

                    JSONObject province = jb.getJSONObject("province");
                    String provincename = province.getString("name");
                    Log.e("provincename", provincename);

                    JSONObject town = jb.getJSONObject("town");
                    String townname = town.getString("name");
                    Log.e("townname", townname);
                    User users = new User();
                    users.setUsername(consignee);
                    users.setMobile(mobile);

                    users.setAddress(provincename + districtname + cityname + address + townname);
                    datas.add(users);
                }
                ls.setAdapter(new LocationAdapter(datas));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
}
